﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Management;
using System.Windows.Data;
using System.Threading.Tasks;
using System.Diagnostics;
using CloudReader.Net.Server;
using System.Configuration;

namespace CloudReader.Server.Runtime
{
    public class ServerRuntime : INotifyPropertyChanged
    {
        Core.ServerBackEnd server = new Core.ServerBackEnd();
        public event PropertyChangedEventHandler PropertyChanged;



        public ServerRuntime()
        {

            TotalAvaliableConnection = MaxConnection = int.Parse(ConfigurationManager.AppSettings["MaxConnection"]);
            string st = "";
            ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                st = mo["TotalPhysicalMemory"].ToString();
            }
            TotalMemory = (int)(long.Parse(st) / 1048576);
            Process p = Process.GetCurrentProcess();
            if (Environment.Is64BitOperatingSystem)
            {
                UsedMemory = (int)p.PrivateMemorySize64 / 1048576;
            }
            else
            {
                UsedMemory = p.PrivateMemorySize / 1048576;
            }
            //数据同步
            Task DataSync = new Task(async () =>
            {
                while (server != null)
                {
                    await Task.Delay(1000);
                    CurrentConnection = server.CurrentConnection;
                    CurrentAvaliableConnection = server.CurrentAvaliableConnection;
                    MaxConnection = TotalAvaliableConnection = server.TotalAvaliableConnection;
                    if (server.EventPool.Count > 0)
                    {
                        lock(server.EventPool)
                        {
                            while(server.EventPool.Count>0)
                            {
                                Event.Enqueue(server.EventPool.Dequeue());
                            }
                        }
                    }
                    if (server.ExceptionPool.Count > 0)
                    {
                        lock (server.ExceptionPool)
                        {
                            while (server.ExceptionPool.Count > 0)
                            {
                                Event.Enqueue(server.ExceptionPool.Dequeue());
                            }
                        }
                    }
                }
            });
            DataSync.Start();
        }

        public static int MaxConnection { get; set; }
        public static int CurrentConnection { get; set; }
        public static int TotalAvaliableConnection { get; set; }
        public static int CurrentAvaliableConnection { get; set; }
        public static int TotalMemory { get; set; }
        public static int UsedMemory { get; set; }
        public static int MaxExecuteFrame { get; set; } = 50;
        public static int CurrentExecuteFrame { get; set; }

        public static IMultiValueConverter Angel { get; set; } = new Core.SpinAngle();

        public Queue<string> Event { get; set; } = new Queue<string>();
        public Queue<string> ExceptionPool { get; set; } = new  Queue<string>();
        public void Update()
        {
            var member = new[]
            {
                "Event",
                "ExceptionPool"
            };
            foreach (var i in member)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(i));
            }
        }
    }
}
